package com.kzd.btgame.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPStaticUtils
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.app.BUNDLE_KEY_SEARCH
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentSearchRecordBinding
import com.kzd.btgame.jetpack.vm.SearchViewModel
import com.kzd.btgame.main.home.adapter.HomeSearchGuessAdapter
import com.kzd.btgame.main.home.adapter.HomeSearchLabelAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class SearchRecordFragment : OwnerViewBindingFragment<FragmentSearchRecordBinding>() {

    companion object {
        fun newInstance(): SearchRecordFragment {
            return SearchRecordFragment()
        }
    }

    private val mHomeSearchRecordAdapter = HomeSearchLabelAdapter()
    private val mHomeSearchHotAdapter = HomeSearchLabelAdapter()
    private val mHomeSearchGuessAdapter = HomeSearchGuessAdapter()

    private val mViewModel:SearchViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentSearchRecordBinding
        get() = FragmentSearchRecordBinding::inflate

    override fun initView() {
        mViewBinding.rvSearchRecordRecordBody.apply {
            val layoutManager = FlexboxLayoutManager(
                requireContext(), FlexDirection.ROW, FlexWrap.WRAP
            )
            this.layoutManager = layoutManager
        }.adapter = mHomeSearchRecordAdapter.apply {
            setOnItemClickListener { _, _, position ->
                mViewModel.keywordLiveData.postValue(data[position])
            }
        }

        mViewBinding.rvSearchRecordHotBody.apply {
            val layoutManager = FlexboxLayoutManager(
                requireContext(), FlexDirection.ROW, FlexWrap.WRAP
            )
            this.layoutManager = layoutManager
        }.adapter = mHomeSearchHotAdapter.apply {
            setOnItemClickListener { _, _, position ->
                mViewModel.keywordLiveData.postValue(data[position])
            }
        }

        mViewBinding.rvSearchRecordGameBody.apply {
            val layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            this.layoutManager = layoutManager
        }.adapter = mHomeSearchGuessAdapter

        mViewBinding.ivSearchRecordClear.setOnClickAntiShakeListener {
            SPStaticUtils.remove(BUNDLE_KEY_SEARCH)
            mViewModel.searchRecord()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden)
            return
        mViewModel.searchRecord()
    }

    override fun initViewMode() {
        mViewModel.searchRecordLiveData.observe(viewLifecycleOwner, { record ->
            if (record.isNullOrEmpty()) {
                mViewBinding.clSearchRecordBody.visibility = View.GONE
            } else {
                mViewBinding.clSearchRecordBody.visibility = View.VISIBLE
                mHomeSearchRecordAdapter.setNewInstance(record)
            }
        })

        mViewModel.searchHotLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val hots = mutableListOf<String>()
            data.forEach { hots.add(it.name) }
            mHomeSearchHotAdapter.setNewInstance(hots)
        })

        mViewModel.searchDefaultLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mHomeSearchGuessAdapter.setNewInstance(data)
        })
    }
}